Python / Advanced / Send Whatsapp Message
Send Whatsapp Message
-
Approch 1: pywhatkit
pywhatkit package
pip install pywhatkit Usage
import pywhatkit import time import datetime from datetime import timedelta try: def sendMessage(number,msg): # sending message in Whatsapp in India so using Indian dial code (+91) now = datetime.datetime.today() now = now + timedelta(minutes=1) h = now.hour m = now.minute print(h) pywhatkit.sendwhatmsg_instantly(number, msg, 8, tab_close=True) #pywhatkit.sendwhatmsg(number, msg, h, m, 8 , True, 1) #pywhatkit.sendwhatmsg_to_group('Nutmeg County Trip', msg, h, m, 30 , False, 1) #sendMessage("+918907689346","Hi Guys Good Evening1") #sendMessage("+918907689346","Hi Guys Good Evening2") #sendMessage("+918907689346","2") #sendMessage("+918907689346","3") #sendMessage("+918907689346","4") #sendMessage("+918907689346","5") with open('message.txt', 'r', encoding="utf-8") as f: msg = f.read() with open('recipients.txt', 'r') as f: groups = [group.strip() for group in f.readlines()] for group in groups: sendMessage(group,msg) # time.sleep(30) # t4.start() # time.sleep(30) # t5.start() # now = datetime.datetime.today() # now = now + timedelta(minutes=1) # h = now.hour # m = now.minute # pywhatkit.sendwhatmsg("+918907689346", "Good evening Vineetha2", h, m, 15 , True, 1) # now = datetime.datetime.today() # now = now + timedelta(minutes=1) # h = now.hour # m = now.minute # pywhatkit.sendwhatmsg("+918907689346", "Good evening Vineetha3", h, m, 15 , True, 1) # now = datetime.datetime.today() # now = now + timedelta(minutes=1) # h = now.hour # m = now.minute # pywhatkit.sendwhatmsg("+918907689346", "Good evening Vineetha4", h, m, 15 , True, 1) print("Message Sent!") #Prints success message in console # error message except: print("Error in sending the message") Browser window will not be opened as background task. -
Approch 2: pyautogui
import pyautogui as pg import webbrowser as web import time import pandas as pd import base64 #data = pd.read_csv("leads.csv") #data_dict = data.to_dict('list') #leads = data_dict['MobileNumber'] #messages = data_dict['Message'] #combo = zip(leads,messages) first = True with open('message.txt', 'r', encoding="utf-8") as f: message = f.read() with open('recipients.txt', 'r') as f: groups = [group.strip() for group in f.readlines()] for group in groups: time.sleep(4) web.open("https://web.whatsapp.com/send?phone="+group+"&text="+message) if first: time.sleep(3) first=False width,height = pg.size() pg.click(width/2,height/2) time.sleep(7) pg.press('enter') time.sleep(3) pg.hotkey('ctrl', 'w') Browser window will be opened as background task. -
Approch 3: Selenium
Selenium package
pip install Selenium usage
1. download chrome browse.exe
2. keep chrome.exe in the project folder
3. add target (group name or person name)
target = '"Vineetha"' 4. find group name in the left panel
x_arg = '//span[contains(@title,' + target + ')]' group_title = wait.until(EC.presence_of_element_located(( By.XPATH, x_arg))) 5. click on group name
group_title.click() 6. find text editor
inp_xpath = '//div[@contenteditable="true"][@data-tab="10"]' input_box = wait.until(EC.presence_of_element_located(( By.XPATH, inp_xpath))) 7. send text
input_box.send_keys(string + Keys.ENTER) 8. find file browsing button
attachment_xpath='//div[@title="Attach"]' attachment_box = wait.until(EC.presence_of_element_located(( By.XPATH, attachment_xpath))) 9. browse file
attachment_box.click() 10. find and assign image path to input
image_xpath='//input[@accept="image/*,video/mp4,video/3gpp,video/quicktime"]' image_box = wait.until(EC.presence_of_element_located(( By.XPATH, image_xpath))) image_box.send_keys('E:\\python\\ser-2.png') 11. find send button and send image
Complete codesend_xpath='//span[@data-icon="send"]' send_button= wait.until(EC.presence_of_element_located(( By.XPATH, send_xpath))) send_button.click() from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By import time # Replace below path with the absolute path # to chromedriver in your computer driver = webdriver.Chrome() driver.get("https://web.whatsapp.com/") wait = WebDriverWait(driver, 600) # Replace 'Friend's Name' with the name of your friend # or the name of a group target = '"Vineetha"' # Replace the below string with your own message string = "Hi Guys Good Evening!!!" x_arg = '//span[contains(@title,' + target + ')]' group_title = wait.until(EC.presence_of_element_located(( By.XPATH, x_arg))) group_title.click() inp_xpath = '//div[@contenteditable="true"][@data-tab="10"]' input_box = wait.until(EC.presence_of_element_located(( By.XPATH, inp_xpath))) attachment_xpath='//div[@title="Attach"]' attachment_box = wait.until(EC.presence_of_element_located(( By.XPATH, attachment_xpath))) attachment_box.click() image_xpath='//input[@accept="image/*,video/mp4,video/3gpp,video/quicktime"]' image_box = wait.until(EC.presence_of_element_located(( By.XPATH, image_xpath))) image_box.send_keys('E:\\python\\ser-2.png') send_xpath='//span[@data-icon="send"]' send_button= wait.until(EC.presence_of_element_located(( By.XPATH, send_xpath))) send_button.click() for i in range(5): input_box.send_keys(string + Keys.ENTER) time.sleep(1) #driver.close()